<?php
namespace Home\Controller;
use Think\Page;
use Think\Upload;

/**
 * 校本课程申报类控制器
 * @author水月居 <singliang@163.com>
 */
class CurriculumController extends HomeController {

    /**
     * 用户管理首页
    */
    public function index(){

		
		//echo "查看已申报课程";

		$sclass = I('class')?I('class'):"请选择班级" ; //设置学生班级
		$where=array("status"=>"1","year"=>C('YEAR'));		
        $curriculum=D("Curriculum");
		$list=$curriculum
        //->field("sid, grade, class, number, name, sex, cid, status")
        ->order("category asc")->where($where)->select();
		
	    //dump($category);
        //$cateid=array($category['id'],true);
		$this->assign('_curriculum', $list);
        $this->assign('_categoryid', $cateid);
       
        $this->meta_title = '课程信息';
        $this->display();
    }
    /* "添加课程"*/
   public function add(){
	   
        $this->display();

    }

    /**
     * 查看学生信息
     * @param  integer $cid 课程ID
     * @return string       
     */
    public function student($cid = 0){
       //获取课程信息
        $curriculum=D("Curriculum");
        $where['year']=I('year')?I('year'):C('YEAR');
        $clist=$curriculum->field("cid, cname")->where($where)->select();
        //获取学生信息
         $map['cid'] =I('cid')?I('cid'):$cid;//获取cid
        $student=D('student');
        $order=array('class'=>'asc','sid'=>'asc');
       
        $stlist=$student->field("sid,grade,code,class,name,sex,update_time")->order($order)->where($map)->limit(60)->select();
	    //检查重新统计人数
        $curr=M('curriculum');          
            $curdata['count']=$student->where($map)->count();
            if($cur['count']!=$curdata['count']){
            $curr->where($map)->save($curdata);
            }

        //获取当前课程信息
        $cur=$curriculum->where($map)->find();        
        
        $this->assign('_cur', $cur);
        $this->assign('_curriculum', $clist);
        $this->assign('student', $stlist);
        $this->display();

    }
    public function showCurriculum($cid=0){
        $cid=I('cid')?I('cid'):$cid;//获取cid
        //dump($cid);
        $curriculum=D("Curriculum");
        $cur=$curriculum->find($cid);
        //dump($cur);
        $this->assign('_cur', $cur);
        $this->display();
    }
    public function edit($cid=0){
        $cid=I('cid')?I('cid'):$cid;//获取cid
         $curriculum=D("Curriculum");
         if(IS_POST){
             $curriculum->create();
             //$se=I('post.cid');
              //dump($curriculum->create());
             $curriculum->save();
             }       
        $cur=$curriculum->find($cid);
        $this->assign('_cur', $cur);
        $this->display();             
    }


    //保存学生选课信息
    public function submitCurriculum($cid=0){
       $cid=I('cid')?I('cid'):$cid;//获取cid
    
     }




    //显示资料上传列表
    public function uploadindex(){   

        $where=array("status"=>"1","year"=>C('YEAR'));      
        $curriculum=D("Curriculum");
        $list=$curriculum
        //->field("cid, teacher, name, uid")
        ->order("category asc")->where($where)->select();
        
        //dump($list);
        //$cateid=array($category['id'],true);
        $this->assign('_curriculum', $list);       
        $this->meta_title = '课程信息';
        $this->display();
    }
    public function showUpload(){
		 $order=array('id'=>'desc','cid'=>'asc');
		 $map["cid"]=I('cid')?I('cid'):0;//获取cid
		 if(!$map["cid"]) unset($map["cid"]);

		 $map['year']=C('YEAR');
    	$kecheng=D('kecheng')->field('cate,cid,cname,teacher,update_time,filename,url,uid')->where($map)->order($order)->select();
    	//dump($kecheng);
    	$this->assign('kecheng',$kecheng);
    	$this->display();
    }
//上传资料 

   public function upload(){
      $cid=I('cid')?I('cid'):$cid;//获取cid
          if(IS_POST){
            if(!$_POST['cate']){
                $this->error("请选择上传类别后再上传");
            }
            //header("Content-type: text/html; charset=utf-8");           

           /**如果目录不存在则自动创建**/
            $root_path= './Uploads/Xiaoben/';
            $uppath   = $root_path.C('YEAR');
            $uppath_gk=iconv('utf-8', 'gbk', $uppath);  //将目录转化gbk  
            if(!file_exists($uppath_gk)) {
                $up='./Uploads/Xiaoben/';
                if(!file_exists($up)) mkdir($up);
                $up.= '/'.C('YEAR');
                if(!file_exists($up)) mkdir($up);
                mkdir($uppath_gk);//如果不存在则设定目录
                echo '目录创建成功';
             }

       $save=I('cate').I('cid').I('cname').I('teacher').date(ymd);//保存文件的命名规则

       $upload = new \Think\Upload();//实例化上传类   
       $upload->maxSize  = 30*1024*1024 ; // 设置附件上传大小 
       $upload->exts      =     array('zip','rar','doc','docx','ppt','pptx');// 设置附件上传类型
       $upload->rootPath  =      $root_path.C('YEAR').'/'; // 设置附件上传根目录
       $upload->savePath  = ''; // 设置附件上传（子）目录   
       $upload->autoSub  = false; 
        $upload->saveName=iconv('utf-8', 'gbk', $save); //保存文件的命名规则
        $upload->Replace=true;
        $info   =  $upload->upload();

				 //$file=$_FILES['upfile'];
				 $file['ext']    =   pathinfo($_FILES['upfile']['name'], PATHINFO_EXTENSION);

				    if(!$info) {// 上传错误提示错误信息
				        $this->error($upload->getError());
				    }else{// 上传成功
				  //保存表单数据
				        $url= C('YEAR').'/'.$save.'.'.$file['ext'];      
				    	$data=array(    
				    		         "url" => $url,
				    		         'cate'=> I('cate'),
				                     "cid" =>   I('cid'),
				                     "teacher"=>I('teacher'),
				                     "filename"=>$_FILES['upfile']['name'],
				                     "cname"     => $_POST["cname"],  
				                     "uid" =>   I('uid'),
				                     "year"=>C('YEAR'),
				                     "update_time" => date(Ymd)
				                     ); 

				             //  dump($data) ;            
				           $kecheng=D('kecheng');
				             $kecheng->create($data);
				             $kecheng->add(); 
				             //dump($kecheng->getLastSql());
				             
				        $this->success('上传成功！');
				    }
             } 
        $cur=D("Curriculum")->find($cid);
        $this->assign('_cur', $cur);
        //dump($cur);
	   $this->display(); 
   
   }
	/*编辑优秀学员*/
	public function outstand(){
         $cid=78;
		  $cid=I('cid')?I('cid'):$cid;//获取cid
		  $map['year']=C('YEAR');
		  $map['cid']=$cid;//$cid;
		  //获取当前课程学生
	     $studata=M('student')->where($map)->field('sid,class,code,name,sex,outstand')->select();
	     //dump($studata);
	     $this->assign('student',$studata);
	     //获取当前课程信息
		  $cur=D('Curriculum')->find($cid);
         $this->assign('_cur', $cur);
          if(IS_POST){
          	dump($_POST);
          }
          $this->display();

	}
		/*编辑优秀学员*/
	public function score(){
		  $cid=I('cid')?I('cid'):$cid;//获取cid
		  $map['year']=C('YEAR');
		  $map['cid']=$cid;
		  $where['year']=C('YEAR');
		  $list=M('curriculum')
        ->field("cid, cname, teacher,uid")
        ->order("category asc")->where($where)->select();
       // dump($list);
		 
		  //获取当前课程学生
	     $studata=M('student')->where($map)->field('sid,class,code,name,sex,score,outstand,scoreuptime')->select();
	     //dump($studata);
	     $this->assign('student',$studata);
	     //获取当前课程信息
		  $cur=D('Curriculum')->find($cid);
         $this->assign('_cur', $cur);
         $this->assign('list',$list);
          if(IS_POST){
          	$p=$_POST;
            //转换数组为更新格式
          	for($i=1;$i<=count($p['sid']);++$i){          		
          	$data[$i]['sid']=$p['sid'][$i];
          	$data[$i]['score']=$p['score'][$i];
          	$data[$i]['outstand']=$p['out'][$i];
          	$data[$i]['scoreuptime'] = NOW_TIME;        
             }         
        	//dump($data);
        	//exit;
                // $student->create($data);
          //$student->save($data);
          //echo $student->getLastSql() ;

          	$student=M('student');
        
         foreach ($data as $v) {          

         	if($student->save($v)=== false) $this->error("数据保存失败");
            }
              $this->success('更新成功', U('Curriculum/score'));

          }
          $this->display();

	}





}